/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.nicmodule.services;

import org.testis.nicmodule.core.NetworkInterfaceController;
import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.LogSenderService;
import org.testis.nicmodule.data.NicModuleDataObject;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import java.lang.reflect.InvocationTargetException;
import org.testis.logging.AtofLogLevel;
import org.testis.nicmodule.core.NetworkInterfaceControllerFactory;
import org.testis.nicmodule.core.NicModuleException;

/**main method for setting interface parameters
 *
 * @author root
 */
public class NicModuleSetAndVerifyInterfaceConfigurationAction extends AtofModuleAction {

    @Override
    public boolean operateRequest(AtofModuleService request) {
        NetworkInterfaceController tmpObject;
        NicModuleDataObject dataObj = (NicModuleDataObject) request.getDataObj();
        try {

            LogSenderService.putMessageOnQueue(
                    "NicModuleSetAndVerifyInterfaceConfigurationAction",
                    AtofLogLevel.INFO,
                    "Set and verify interface configuration requested",
                    dataObj.getMessageId());

            String iFace = dataObj.getParamInterface();
            String iModule = dataObj.getParamModule();
 
            tmpObject = NetworkInterfaceControllerFactory.createInstance(iModule,
                    iFace);
            if (!tmpObject.isInterfaceExists()) {
                throw new TestStepException(TestStepResultType.MISTAKE,
                        "Execution of NicModuleVerifyInterface failed...Interface does not exist:" + iFace);
            }
            boolean result =
                    tmpObject.setAndVerifyInterfaceConfiguration(dataObj.getIP(),
                    dataObj.getGateway(),
                    dataObj.getDNS(),
                    dataObj.getSubnet(),
                    dataObj.isFromDhcp(), dataObj.getMessageId());
            AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;

            LogSenderService.putMessageOnQueue(
                    "NicModuleSetAndVerifyInterfaceConfigurationAction",
                    level,
                    "NicModuleSetAndVerifyInterfaceConfigurationAction resulted:"
                    + Boolean.valueOf(result),
                    dataObj.getMessageId());
            return result;

        } catch (IllegalArgumentException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction failed..."
                    + ex.getMessage());

        } catch (InvocationTargetException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction failed..."
                    + ex.getMessage());

        } catch (InstantiationException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction failed..."
                    + ex.getMessage());
        } catch (IllegalAccessException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction failed..."
                    + ex.getMessage());

        } catch (ClassNotFoundException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction failed..."
                    + ex.getMessage());
        } catch (NicModuleException ex) {
            throw new TestStepException(TestStepResultType.MISTAKE,
                    "Execution of NicModuleSetAndVerifyInterfaceConfigurationAction due to NicmoduleException..."
                    + ex.getMessage());
        }

    }
}
